from scrapyProject.spiders.spiderBase import spiderBase
import json
import re
import chardet
import requests

#需要填入cook消息就可以采集国内的机票，国际机票没有破解
class ctrip_spider(spiderBase,):
    name = 'ctrip'
    def start_requests(self):
        urls=["https://flights.ctrip.com/itinerary/api/poi/get"]
        urls=["https://flights.ctrip.com/itinerary/oneway/bjs-sha?date=2019-01-17"]
        meta = {'cookiejar': True}
        for url in urls:
            yield self.RequestUrl(url=url, callback=self.parse, isMustParse=True, meta=meta)

    def get_xcelement(self, from_city, to_city, response):
        # print(response)
        pattern = re.compile('.*?data.*?\|(.*?)\((.*?)\).*?', re.S)
        city = re.findall(pattern, response)
        # print(r)
        city = dict(city)  # 转换成字典
        # 更改两个不对的地方
        city['北京'] = 'BJS'
        city['上海'] = 'SHA'

        pattern = re.compile('.*?data.*?\|(.*?)\(.*?\|(\d+)\|.*?', re.S)
        cityid = re.findall(pattern, response)
        cityid = dict(cityid)
        # print(cityid)

        fcity = city[from_city].lower()
        tcity = city[to_city].lower()
        fctiy_id = int(cityid[from_city])
        tcity_id = int(cityid[to_city])
        # print(fcity,tcity,fctiy_id,tcity_id)

        # 返回的数据：出发地英文缩写，到达地英文缩写，出发地id编号，到达地id编号
        return [fcity, tcity, fctiy_id, tcity_id]

    def parse(self, response):
        Cookie1 = response.headers.getlist('Set-Cookie')
        Cookie = response.request.headers.getlist('Cookie')
        #jsonData = json.dumps(response.body)
        from_city="北京"
        to_city="上海"
        date="2019-1-29"
        strData = response.body.decode()
        element = self.get_xcelement(from_city, to_city, strData)
        payloadData = {
            "flightWay": "Oneway",
            "classType": "ALL",
            "hasChild": 'false',
            "hasBaby": 'false',
            "searchIndex": "1",
            "airportParams": [{"dcity": element[0], "acity": element[1], "dcityname": from_city, "acityname": to_city,
                               "date": date, "dcityid": str(element[2]), "acityid": str(element[3])}]
        }
        url = 'http://flights.ctrip.com/itinerary/api/12808/products'
        payloadData["airportParams"] = json.dumps(payloadData["airportParams"])

        response.meta['cookiejar']
        yield self.FromRequestUrl(url=url, callback=self.parseItem, isMustParse=True, formdata = payloadData)
    def parseItem(self, response):
        print(response.body.decode())
        pass
